﻿package com.sff.net {
		
	import com.sff.core.sff_internal;
	import com.sff.events.LoaderEvent;
	import com.sff.net.BaseLoaderItem;
	
	import flash.events.Event;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;	
	
	use namespace sff_internal;
	
	public class LoaderItem extends BaseLoaderItem {

		public var loader : Loader;
		
		public var useInit : Boolean = false;
		
		
		public function LoaderItem ( request : URLRequest, context : LoaderContext = null, params : Object = null, statusMessage : String = null) : void {
			super( request, context, params, statusMessage);
			
			loader = new Loader( );
			register( loader.contentLoaderInfo );
		}

		override public function execute () : void {
			super.execute();
			loader.load( _request, _context );
		}		

		override public function dispose () : void {
			try {	
				loader.close( );
				loader.unload( );
			} catch( e : Error ) {};
			
			unregister( );
			dispatchEvent( new LoaderEvent( LoaderEvent.DISPOSED, this ) );
			
			super.dispose();
		}

		override protected function onInit(e : Event) : void {
			super.onInit( e );
			if( useInit ) {
				dispatchEvent( new Event( Event.COMPLETE ) );
			}
		}
	}
}